/**
 * 入口文件
 */
const express = require('express')
const path = require('path')
const bodyparser = require('body-parser')
const session = require("express-session");
const router1 = require('./src/controller/BlogController')
const router2 = require('./src/controller/LoginController')
const router3 = require('./src/controller/BackController')

const app = express()
app.listen(3000, err => {
        if (err) console.log(err);
        console.log('服务器已启动：http://localhost:3000');

    })
    //配置session
app.use(session({    
    secret: "login",
    name: 'session_id',
    resave:  false,
    saveUninitialized:  true,
     cookie: ('name',  'value', { maxAge:  5 * 60 * 1000, secure:  false })
}));


/*使用中间件*/
//配置静态资源路径
app.use(express.static(path.join(__dirname, 'public')))


app.engine('html', require('express-art-template'));
app.set('views', path.join(__dirname, './src/views'))
app.use(bodyparser.urlencoded({ extended: false }));
app.use(bodyparser.json());
app.use(router1)
app.use(router2)
    //登录拦截器，必须放在静态资源声明之后、路由导航之前
app.use(function(req, res, next) {
    var url = req.originalUrl;
    if (!req.session.admin) {
        return res.redirect("/login");
    }
    next();
});
app.use(router3)